package org.codetranslators.common;

public class Temp 
{
	public static final int TEMP = 0;
	public static final int REGISTER = 1;
	public static final int USER_DEFINED = 2;
	
	public static Temp fpTemp;
	public static Temp spTemp;
	public static Temp a0Temp;
	public static Temp a1Temp;
	public static Temp a2Temp;
	public static Temp a3Temp;
	public static Temp v0Temp;
	public static Temp v1Temp;
	
	private static int count;  
	private int num;
	private int tempType;
	private String registerType;
	
	private String userDefinedString;
	
	public Temp() { 
		tempType = TEMP;
	    num = count++;
	}
	
	public Temp(int num, String userDefinedString){
		tempType = USER_DEFINED;
		this.num = num;
		this.userDefinedString = userDefinedString;
	}
	
	private Temp(String type)
	{
		tempType = REGISTER;
		registerType = type;
	}
	
	/**
	 * Resets the count of the temporary variables to zero; typically called when compilation is
	 * repeated afresh in the same running instance
	 *
	 */
	public static void reset(){
		count = 0;
	}
	
	public static Temp getTemp(String type)
	{
		if(type.equals("FP"))
		{
			if(fpTemp == null) fpTemp = new Temp("fp");
			return fpTemp;
		}
		else if(type.equals("SP"))
		{
			if(spTemp == null) spTemp = new Temp("sp");
			return spTemp;
		}
		else if(type.equals("a0"))
		{
			if(a0Temp == null) a0Temp = new Temp("a0");
			return a0Temp;
		}
		else if(type.equals("a1"))
		{
			if(a1Temp == null) a1Temp = new Temp("a1");
			return a1Temp;
		}
		else if(type.equals("a2"))
		{
			if(a2Temp == null) a2Temp = new Temp("a2");
			return a2Temp;
		}
		else if(type.equals("a3"))
		{
			if(a3Temp == null) a3Temp = new Temp("a3");
			return a3Temp;
		}
		else if(type.equals("v0"))
		{
			if(v0Temp == null) v0Temp = new Temp("v0");
			return v0Temp;
		}
		else if(type.equals("v1"))
		{
			if(v1Temp == null) v1Temp = new Temp("v1");
			return v1Temp;
		}
		
		return null;
	}
	
	public String toString() 
	{
		if(tempType == TEMP)
			return "t" + num;
		else if(tempType == REGISTER)
			return "$" + registerType;
		else
			return userDefinedString;
	}
	
	public int getNum(){
		return num;
	}
	
	public int getTempType()
	{
		return tempType;
	}
	
	public boolean isPhysicalRegister()
	{
		return tempType == REGISTER;
	}
	
	public String getRegisterType()
	{
		return registerType;
	}
	
	public boolean equals(Temp temp)
	{
		if(this.userDefinedString.equalsIgnoreCase(temp.toString())) return true;
		return false;
	}
}
